import _ from 'lodash';
import { routerRedux } from 'dva/router';
import moment from 'moment';
import * as appointmentService from '../services/appointmentService';

export default {
  namespace: 'appointment',
  state: {
    appointmentSetting: [],
    appointmentResult: {},
    submited: false,
    currentVip: null,
  },
  effects: {
    * fetchAppointment({ payload }, { call, put, select }) {
      const { shopId } = yield select(state => state.auth.currentUser);
      const result = yield call(appointmentService.fetchAppointment, payload.shopId || shopId, 1);
      yield put({ type: 'saveAppointmentSetting', payload: result.data });
    },
    * submitAppointment({ payload }, { call, put, select }) {
      const { shopId, mobile, vip_name, card_no } = yield select(state => state.appointment.currentVip);
      yield call(appointmentService.submitAppointment, { ...payload, shopId, mobile, vip_name, card_no });
      yield put({ type: 'saveSubmited', payload: true });
    },

    * fetchAppointmentResult({ payload }, { call, put, select }) {
      let currentVip = yield select(state => state.appointment.currentVip);
      const shopId = currentVip && currentVip.shopId?currentVip.shopId:payload.shopId;
      const card_no = currentVip && currentVip.card_no?currentVip.card_no:payload.card_no;
      const result = yield call(appointmentService.fetchAppointmentResult, shopId, card_no);
      console.log(result);
      yield put({ type: 'saveAppointmentResult', payload: result.data });
    },

    * cancelAppointment({ payload }, { call, put, select }) {
      yield call(appointmentService.cancelAppointment, payload._id, '2');
      yield put({ type: 'fetchAppointmentResult', payload: {} });
    },

    * fetchCurrentVip({ payload }, { call, put, select }) {
      const result = yield call(appointmentService.fetchCurrentVip, payload.shopId, payload.card_no);
      yield put({ type: 'saveCurrentVip', payload: result.data });
    },
  },
  reducers: {
    saveAppointmentSetting(state, { payload }) {
      return { ...state, appointmentSetting: payload };
    },
    saveBranches(state, { payload }) {
      return { ...state, branches: payload };
    },
    saveAppointmentResult(state, { payload }) {
      return { ...state, appointmentResult: payload };
    },
    saveSubmited(state, { payload }) {
      return { ...state, submited: payload };
    },
    saveCurrentVip(state, { payload }) {
      return { ...state, currentVip: payload };
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/appointment') {
          dispatch({ type: 'fetchAppointment', payload: { shopId: query.shopId } });
          dispatch({ type: 'fetchCurrentVip', payload: { shopId: query.shopId, card_no: query.card_no } });
        }
        if (pathname === '/appointmentResult') {
          dispatch({ type: 'fetchCurrentVip', payload: { shopId: query.shopId, card_no: query.card_no } });
        }
      });
    },
  },
};
